import { _decorator, Component, Node, Prefab } from 'cc';
import { UIBase } from './ui_base';
import { PoolManager } from '../../farmework/pool_manager';
import { ResourceUtil } from '../../farmework/resource_util';
import { UIManagerDefines } from '../../farmework/ui_manager_define';
const { ccclass, property } = _decorator;

/**
 * 跑马灯... 需要设置为全局节点
 */
@ccclass('UIMarqueePanel')
export class UIMarqueePanel extends UIBase {
    private _marqueeQuene: Array<UIManagerDefines.MarqueeOptions> = [];
    private _currentNode: Node | null = null;
    private _isLoad: boolean = false;
    private _abortAsync = false;

    /**
     * 添加跑马灯
     * @param marquee 
     */
    public addMarquee (marquee: UIManagerDefines.MarqueeOptions) {
        if (marquee.top) {
            if (this._isLoad) {
                this._abortAsync = true;
                this._marqueeQuene.unshift(marquee)
            } else {
                this.showMarquee(marquee);
            }
        } else {
            this._marqueeQuene.push(marquee);
        }
    }

    update () {
        if (!this._isLoad && !this._currentNode) {
            if (this._marqueeQuene.length > 0) {
                this.showMarquee(this._marqueeQuene.shift()!)
            }
        }
    }

    /**
     * 展示跑马灯
     * @param marquee 
     */
    async showMarquee (marquee: UIManagerDefines.MarqueeOptions) {
        this._abortAsync = false;
        if (this._currentNode) {
            this._currentNode.getComponent(UIBase)?.setDisableCall(null);
            PoolManager.instance.putNode(this._currentNode);
            this._currentNode = null;
        }

        this._isLoad = true;
        let pf = await ResourceUtil.getUIPrefabRes(marquee!.path!) as Prefab;
        this._currentNode = PoolManager.instance.getNode(pf, this.node);
        this._isLoad = false;

        // 强制中断
        if (this._abortAsync) {
            this.showMarquee(this._marqueeQuene.shift()!);
            return;
        }

        let uiBase = this._currentNode.getComponent(UIBase);
        if (marquee?.path == UIManagerDefines.DEFAULT_PATH.MARQUEE) {
            uiBase?.show(marquee.content);
        } else {
            let uiBase = this._currentNode.getComponent(UIBase);
            let args = [marquee.content];
            if (marquee.args && marquee.args.length > 0) {
                args = args.concat(marquee.args)
            }
            uiBase?.show.apply(uiBase, args);
        }

        uiBase?.setDisableCall(() => {
            // if (this._marqueeQuene.length > 0) {
            //     this.showMarquee(this._marqueeQuene.shift()!);
            // } else {
            PoolManager.instance.putNode(this._currentNode!);
            this._currentNode = null;
            //  }
        });
    }

    /**
     * 清除跑马灯
     */
    public clear () {
        if (this._currentNode) {
            PoolManager.instance.putNode(this._currentNode!);
            this._currentNode = null;
        }
        this._marqueeQuene.length = 0;
    }
}

